package com.example.ApiController;

import com.example.common.Result;
import com.example.entity.Attendance;
import com.example.entity.RequestData;
import com.example.entity.Student;
import com.example.service.AttendanceService;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/attendance")
public class AttendanceApiController {

    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private StudentService studentService;


    /**
     * 根据老师id查询出老师所带班级学生的考勤情况
     */
    @GetMapping("/getAttendanceByTeacherId")
    public Result getAttendanceByTeacherId(Integer teacherId){
        List<Map<String,String>> map =attendanceService.selectAttendanceByTeacherId(teacherId);
        return Result.success(map);
    }
    /**
     * 根据班长id查询出班长所在班级学生的考勤情况
     */
    @GetMapping("/getAttendanceByMonitorId")
    public Result getAttendanceByMonitorId(Integer monitorId){
        List<Map<String,String>> map =attendanceService.selectAttendanceByMonitorId(monitorId);
        return Result.success(map);
    }

    /**
     * 根据学生id查询自己所有的考情状况
     */
    @GetMapping("/getAttendanceByStudentId")
    public Result getAttendanceByStudentId(Integer studentId){
        List<Map<String,String>> map =attendanceService.selectAttendanceByStudentId(studentId);
        return Result.success(map);
    }

    /**
     * 查询班级考情信息
     */
    @GetMapping("/getClassAttend")
    public Result getClassAttend(Integer studentId){
        List<Map<String,String>> map =attendanceService.getClassAttend(studentId);
        return Result.success(map);
    }

    /**
     * 根据学生id以及课程id查询出选修这门课的同班学生
     */
    @GetMapping("/getClassmateByCourseId")
    public Result getClassmateByCourseId(Integer studentId,String courseName){
        Student student = studentService.selectById(studentId);
        Integer classId = student.getClassId();
        List<Map<String,String>> map =attendanceService.getClassmateByCourseId(classId,courseName);
        return Result.success(map);
    }

    /**
     * 签到(插入考勤表信息)
     */
    @PostMapping("/add")
    public Result add(@RequestBody RequestData requestData){
        try {
            attendanceService.insert(requestData.getIds(),requestData.getCourseName(),requestData.getStatus());
        }catch (Exception e){
            return Result.error();
        }

        return Result.success();
    }



}
